﻿namespace Vdc.Grid.Design
{
    using Vdc.Grid;
    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Globalization;

    internal class GridMasterDetailSettingsConverter : ExpandableObjectConverter
    {
        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
        {
            return ((sourceType == typeof(string)) || base.CanConvertFrom(context, sourceType));
        }

        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            if (value is string)
            {
                string str = (string) value;
                try
                {
                    string[] strArray = str.Split(new char[] { ',' });
                    return new GridMasterDetailSettings { DetailsCountField = strArray[0].Trim(), LoadingMode = (DetailGridLoadingMode) int.Parse(strArray[1].Trim()), ShowEmptyDetails = bool.Parse(strArray[2].Trim()), State = (DetailGridState) int.Parse(strArray[3].Trim()) };
                }
                catch (Exception exception1)
                {
                    if (exception1 != null)
                    {
                    }
                }
            }
            return base.ConvertFrom(context, culture, value);
        }

        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
        {
            if (destinationType == typeof(string))
            {
                return "";
            }
            return base.ConvertTo(context, culture, value, destinationType);
        }

        public override object CreateInstance(ITypeDescriptorContext context, IDictionary propertyValues)
        {
            return new GridMasterDetailSettings { DetailsCountField = propertyValues["DetailsCountField"].ToString(), LoadingMode = (DetailGridLoadingMode) propertyValues["LoadingMode"], ShowEmptyDetails = bool.Parse(propertyValues["ShowEmptyDetails"].ToString()), State = (DetailGridState) propertyValues["State"] };
        }

        public override bool GetCreateInstanceSupported(ITypeDescriptorContext context)
        {
            return true;
        }
    }
}

